﻿using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace WenSkin.Controls
{
    public class LogListBox : WenControl
    {
        public LogListBox()
        {
            Initcom();
        }
        [Category("Wen"), DefaultValue(true), Description("是否目录下输出日志")]
        public bool LogSave { get; set; } = true;

        [Category("Wen"), DefaultValue(100), Description("日志超过数量自动清理")]
        public int LogClearAutomaticNumber { get; set; } = 100;

        #region 私有属性

        private WenListView logListView;

        #endregion

        #region 初始化
        private void Initcom()
        {
            this.Size = new System.Drawing.Size(500, 200);
            logListView = new WenListView() { Dock = DockStyle.Fill, View = View.Details };

            logListView.Columns.Add("时间", 160);
            logListView.Columns.Add("信息", 300);
            logListView.Columns.Add("状体", 60);
            this.Controls.Add(logListView);

            //logListView.SizeChanged += LogListView_SizeChanged;
            this.SizeChanged += LogListBox_SizeChanged;
        }

        private void LogListBox_SizeChanged(object sender, EventArgs e)
        {
            if (logListView != null)
            {
                int sizeWidth = logListView.Size.Width;
                if (sizeWidth < 300)
                    sizeWidth = 300;
                logListView.Columns[1].Width = sizeWidth - 240;
            }
        }
        #endregion


        #region 表格信息显示插入日志

        public void AddLog(string text, string info = "消息")
        {
            AddFromsInfo(text, info);
        }

        public void AddFormat(string format, params object[] args)
        {
            string str = string.Format(format, args);
            AddFromsInfo(str);
        }
        //插入信息到表格中
        public void AddFromsInfo(string listinfo)
        {
            AddFromsInfo(listinfo, "消息");
        }
        public void AddFromsInfo(string listinfo, string liststate)
        {
            string datetime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            WenListViewItem listViewItem = new WenListViewItem
            {
                Text = datetime
            };
            listViewItem.SubItems.Add(listinfo);
            listViewItem.SubItems.Add(liststate);
            if (this.IsHandleCreated)
            {
                logListView.Invoke(new Action(() =>
                {
                    logListView.Items.Insert(0, listViewItem);
                    //插入日志
                    if (LogSave)
                        InsertLog(datetime, listinfo, liststate);
                    LogClearAutomatic();
                }));
            }
            else
            {
                logListView.Items.Insert(0, listViewItem);
                //插入日志
                if (LogSave)
                    InsertLog(datetime, listinfo, liststate);
                LogClearAutomatic();
            }
        }
        //插入日志
        private void InsertLog(string datetime, string listinfo, string liststate)
        {
            string txtPath = Application.StartupPath + "\\Log\\";
            if (!Directory.Exists(txtPath))
                Directory.CreateDirectory(txtPath);
            File.AppendAllText(string.Format("{0}.txt", txtPath + DateTime.Now.ToString("yyyyMMdd"))
                , string.Format("{0},{1},{2}\r\n", datetime, listinfo, liststate), Encoding.Default);
        }
        #endregion

        /// <summary>
        /// 日志清理
        /// </summary>
        public void LogClear()
        {
            logListView.Items.Clear();
        }

        public void LogClearAutomatic()
        {
            int count = logListView.Items.Count;
            if (LogClearAutomaticNumber < logListView.Items.Count)
            {
                for (int i = LogClearAutomaticNumber; i < count; i++)
                {
                    logListView.Items[i].Remove();
                }
            }
        }
    }
}
